#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: tools_menu.sh
# Author: GID5564
# Description: 菜单
# Created Time: 20/09/23-18:30:24
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash

# 避免重复导入
#[[ -n $__XLIB_IMPORTED__XMENU ]] && return 0
#__XLIB_IMPORTED__XMENU=1

function __xmenu_init__() {

    menu_init
}

#混合字符串占位数
function mixed_string_occupancy(){
    local str=$1
    local str_len=$(echo "$str" | awk '{print length($0)}')
    local hz_num=$(get_HZ_len $str)
    local other_char_num=$((str_len-hz_num))
    local occupancy=$((hz_num*2+other_char_num))
    echo $occupancy
    unset str
    unset str_len
    unset hz_num
    unset other_char_num
    unset occupancy
}

function menu_init(){
    export WINDOW_X=`tput lines` #行数29
    export WINDOW_Y=`tput cols`  #列数63
    export START_X=4 #光标起始位置X
    export TITLE_SPACE_NUM=10 #一行空格总数(4+1+1+4)
    export SHOW_CHAR='*' #显示的特殊字符
    export MENU_ITEM_LEFT_SPACE=6  #菜单项前空格数
    export MENU_ITEM_CHAR=2 #菜单项前后特殊字符数
    export REDUCTION_CONS=4 #多列菜单项间隔缩减常数
    export SINGLE_REDUCTION_CONS=8 #单列菜单项前空格缩减常数
    export COLOR_QUIT=${RED}
    export COLOR_MENU_ITEM=${GREEN}
}

#显示指定数量的特殊字符串
function show_string(){
    [[ -z "$1" ]] && {
        error "Usage: show_string [数量]"
        return $INVALID_ARGUMENTS
    }

    local char=${char:-${SHOW_CHAR}}
    local count="${1:-1}"
    printf "${PURPLE}%*s${RESET}" "${count}" '' | tr ' ' "${char}"

    unset char
    unset count
}


function show_title(){
    local title=$1
    local occ=$(mixed_string_occupancy $title)
    local temp=$((WINDOW_Y-occ-TITLE_SPACE_NUM))
    local char_num=$((temp/2)) #左右两边*数量
    
    local m=$((WINDOW_Y-START_X*2))
    space $START_X
    show_string $m
    printf "\n"

    #左边*号
    space $START_X
    show_string $char_num
    space 1
    
    printf "%s" "${YELLOW}${title}${RESET}"
    space 1
    
    #右边*号
    show_string $char_num
    printf "\n"
    unset title
    unset char_num
    unset temp
    unset occ
}

function show_end(){
    local m=$((WINDOW_Y-START_X*2))
    
    #空4格
    space $START_X
    show_string $m
    printf "\n" 
    space $START_X
    show_string $m
    printf "\n" 
    unset m
}

function show_menu_single(){
    local array_items=(`echo $1|awk -v RS=' ' '{gsub(","," "); print}'`)

    local count=1
    for option in "${array_items[@]}"
    do
        local temp=$(mixed_string_occupancy ${option})
        local space_nums=$((WINDOW_Y-MENU_ITEM_CHAR*2-temp-START_X*2))
        
        local space_t=$((WINDOW_Y-MENU_ITEM_CHAR*2-START_X*2))
        local space_num=$((space_t/2-SINGLE_REDUCTION_CONS))
        local space_r=$((space_nums-space_num))
        
        space $START_X
        show_string ${MENU_ITEM_CHAR}
        space ${space_num}
        if [[ ${count} -eq ${#array_items[@]} ]];then
            printf '%s' ${COLOR_QUIT}${option}${RESET}
        else
            printf '%s' ${COLOR_MENU_ITEM}${option}${RESET}
        fi
        space ${space_r}
        show_string ${MENU_ITEM_CHAR}
        printf "\n"
        let count++
        unset option temp space_nums space_num space_r space_t
    done
    unset count
    unset array_items
}

function show_menu_double(){
    local array_items=(`echo $1|awk -v RS=' ' '{gsub(","," "); print}'`)

    local item_l=()
    local item_r=()
    local item_e=()
    local item_h=()
    local i

    
    local item_num=${#array_items[@]}

    local item_1=$((item_num/2))
    #左列
    item_l=($(echo $array_items | awk -F' ' '{for(i=0;i<NF;i++)if(i%2!=0) print $i}'))
    #右列
    item_r=($(echo $array_items | awk -F' ' '{for(i=2;i<NF;i++)if(i%2==0) print $i}'))
    #退出项
    item_e=($(echo $array_items | awk -F' ' '{print $NF}'))
    
    for ((i=1;i<=$item_1;i++))
    do
        #字符串占位
        local temp_l=$(mixed_string_occupancy ${item_l[$i]})
        [[ -n ${item_r[$i]} ]] && {
            local temp_r=$(mixed_string_occupancy ${item_r[$i]})
        } || {
            local temp_r=0
        }
        #总空格数
        local space_nums=$((WINDOW_Y-MENU_ITEM_CHAR*2-temp_l-temp_r-START_X*2))
        local space_t=$((WINDOW_Y-MENU_ITEM_CHAR*2-START_X*2))
        
        local temp_sl=$((space_t/2-temp_l-REDUCTION_CONS))
        local temp_sr=$((space_nums-MENU_ITEM_LEFT_SPACE-temp_sl))
        space $START_X
        show_string ${MENU_ITEM_CHAR}
        space ${MENU_ITEM_LEFT_SPACE}
        
        printf '%s' "${COLOR_MENU_ITEM}${item_l[$i]}${RESET}"
        space ${temp_sl}
        
        printf '%s' "${GREEN}${item_r[$i]}${RESET}"

        space ${temp_sr}
        show_string ${MENU_ITEM_CHAR}
        printf "\n"
        unset temp_l temp_r space_nums space_t temp_sl temp_sr 
    done
    #字符串占位
    local temp_q=$(mixed_string_occupancy ${item_e})
    
    #空格总数
    local space_nums_q=$((WINDOW_Y-MENU_ITEM_CHAR*2-temp_q-START_X*2))
    local temp_sr_q=$((space_nums_q-MENU_ITEM_LEFT_SPACE))
    
    space $START_X
    show_string ${MENU_ITEM_CHAR}
    space ${MENU_ITEM_LEFT_SPACE}
        
    printf '%s' "${COLOR_QUIT}${item_e}${RESET}"
    space ${temp_sr_q}
        
    show_string ${MENU_ITEM_CHAR}
    printf "\n"

    unset item_e
    unset item_h
    unset i
    unset item_num
    unset item_1
    unset space_nums_q
    unset temp_sr_q
    unset temp_q
    unset array_items
}

function show_menu(){
    local items=$1
    local array_items=(`echo $items | awk -v RS=' ' '{gsub(","," "); print}'`)
    local items_size=${#array_items[@]}
    
    if [[ ${items_size} -gt 6 ]];then
        show_menu_double $items
    else
        show_menu_single $items
    fi
    unset items
    unset array_items
    unset items_size
}
function select_menu(){
    local items=$1
    local api_command_entry=$2
    local menu_name=$3
    local array_items=(`echo $items|awk -v RS=' ' '{gsub(","," "); print}'`)
    local items_size=$((${#array_items[@]}-1))
    
    while [ 1 -eq 1 ]
    do
        Msg "${BLUE}请选择${RESET}${YELLOW}【${menu_name}】${RESET}${BLUE}菜单[${RESET}${RED}0${RESET}${BLUE}-${RESER}${RED}${items_size}${RESET}${BLUE}]:${RESET} "
        read num
        case $num in
            0) break;;
            *) 
                ${api_command_entry} ${num}
                 #功能函数入口，文件api.sh
                    #api_command_entry ${num}
                ;;
        esac
    done
    unset items
    unset array_items
    unset items_size
    unset api_command_entry
}

function set_menu(){
    local title=$1
    local menu_items=$2
    local func_entry=$3
    #菜单标题
    show_title $title
    #菜单项
    show_menu $menu_items
    show_end
    #菜单项功能函数入囗
    select_menu $menu_items $func_entry $title
}

#例子:
function __instance__(){
    
    local title="菜单标题"
    local array_items=("1.系统版本信息","2.本地环境配置","3.搭建开发环境","4.信息安全工具","5.其它常用工具","6.Web学习工具","7.备份与恢复","8.Python项目","0.退出菜单")

    set_menu ${title} ${array_items} '__entry__'
    
    unset title
    unset array_items
}
function __entry__(){
    local id=$1 #功能id
    case ${id} in
    1) wait ;; 
    2) wait ;;
    *)  error "选择无效!"
        ;;
    esac
    unset id
}


__xmenu_init__











































